package com.zzyl.nursing.util;

import com.zzyl.nursing.domain.Contract;
import com.zzyl.nursing.mapper.ContractMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Component
public class ContractCleanTask {
    @Autowired
    ContractMapper contractMapper;

    @Scheduled(cron = "0 0 1 * * ?")
    public void contractTask () {

        log.info("开始判断合同状态，开始时间是否大于等于当前时间");
        //查询所有合同并装在集合中 遍历判断
        List<Contract> contractList = contractMapper.selectContractList(new Contract());
        for (Contract contract : contractList) {
            if (contract.getStartDate().isAfter(LocalDateTime.now())) {
                log.info("合同开始时间在当前时间之后，开始更新合同状态为已生效");
                contract.setStatus(1);
                System.out.println(contract.getContractNumber()+"合同已经开始生效");
                contractMapper.updateContract(contract);
            }
        }
    }
}
